#
# Copyright (c) 2022. China Mobile (SuZhou) Software Technology Co.,Ltd. All rights reserved.
# Lakehouse is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
#

# Usage:
# $ mvn clean install -DskipTests \
#       -Ddocker.hub=${your_docker_hub} \
#       -Ddocker.repo=${your_docker_repo} \
#       -Ddocker.hub.username=${your_docker_hub_username} \
#       -Ddocker.hub.password=${your_docker_hub_password} \
#       -pl lakehouse-engine/engine-meta-crawler -am dockerfile:build dockerfile:push

FROM openjdk:8-jre-slim

ARG JAR_FILE

# Prepare environment
ENV GOSU_VERSION 1.11
ENV FLINK_HOME=/opt/flink
ENV PATH=$FLINK_HOME/bin:$PATH

RUN set -ex && \
    sed -i 's/http:\/\/deb.\(.*\)/https:\/\/deb.\1/g' /etc/apt/sources.list && \
    apt-get update && \
    ln -s /lib /lib64 && \
    apt install -y bash tini libc6 libpam-modules krb5-user libnss3 procps curl && \
    curl -o /usr/local/bin/gosu -L https://github.com/tianon/gosu/releases/download/1.11/gosu-amd64 && \
    chmod 755 /usr/local/bin/gosu && \
    chmod +x /usr/local/bin/gosu && \
    gosu nobody true && \
    curl -o /opt/flink/flink.tgz -L https://mirrors.bfsu.edu.cn/apache/flink/flink-1.12.7/flink-1.12.7-bin-scala_2.12.tgz && \
    groupadd --system --gid=9999 flink && \
    useradd --system --home-dir $FLINK_HOME --uid=9999 --gid=flink flink && \
    cd /opt/flink && \
    tar -xf flink.tgz --strip-components=1 && \
    rm flink.tgz && \
    mkdir /opt/flink/syncplugins/ && \
    chown -R flink:flink .

COPY ${JAR_FILE} /opt/flink/syncplugins/
COPY src/docker/docker-entrypoint.sh /
RUN set -ex && \
  mv /opt/flink/syncplugins/*.jar /opt/flink/syncplugins/metadata-crawler-0.0.1.jar && \
  chmod +x /docker-entrypoint.sh && chown -R flink:flink /docker-entrypoint.sh

WORKDIR $FLINK_HOME

ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 6123 8081
CMD ["help"]
